import LabController from '@/modules/structures/lab'
import SpawnController from '@/modules/structures/spawn'
import { CreepRoles } from '@/modules/structures/spawn/types'
import TowerController from '@/modules/structures/tower'
import { claimerFn } from '@/roles/claimer'
import { crossBuilderFn } from '@/roles/crossBuilder'
import { crossCarrierFn } from '@/roles/crossCarrier'
import { RoomSafeState } from '@/room/state/types'

export default (creep: Creep) => {
    SpawnController.visual(creep)

    if (creep.memory.slack) {
        creep.memory.slack--
        return
    }

    if (creep.memory.boost && !creep.memory.boost.done) {
        LabController.boostCreep(creep)
    }

    switch (creep.memory.role) {
        case 'crossBuilder':
            crossBuilderFn(creep)
            break
        case 'crossCarrier':
            crossCarrierFn(creep)
            break
        case 'claimer':
            claimerFn(creep)
            break
    }

    // 本 tick 没有调用 goTo 方法则删除缓存
    if (!creep._goTo && creep.memory._go) {
        delete creep.memory._go
    }

    if (
        creep.hits < creep.hitsMax &&
        creep.room.controller?.my &&
        (creep.room.memory?.state?.safe?.state === RoomSafeState.normal ||
            [CreepRoles.defender, CreepRoles.teamMember, CreepRoles.builder, CreepRoles.transporter].includes(
                creep.memory.role,
            ))
    ) {
        TowerController.allHeal(creep.room.tower || [], creep)
    }
}
